/**
 * @class drop-down menu - font-family for selected text
 *
 * @param  elRTE  rte   объект-редактор
 * @param  String name  название кнопки
 *
 * @author:    Dmitry Levashov (dio) dio@std42.ru
 * @copyright: Studio 42, http://www.std42.ru
 **/
(function ($) {
    elRTE.prototype.ui.prototype.buttons.fontname = function (rte, name) {
        this.constructor.prototype.constructor.call(this, rte, name);
        var self = this;
        var opts = {
            tpl:'<span style="font-family:%val">%label</span>',
            select:function (v) {
                self.set(v);
            },
            src:{
                '':this.rte.i18n('Font'),
                'andale mono,sans-serif':'Andale Mono',
                'arial,helvetica,sans-serif':'Arial',
                'arial black,gadget,sans-serif':'Arial Black',
                'book antiqua,palatino,sans-serif':'Book Antiqua',
                'comic sans ms,cursive':'Comic Sans MS',
                'courier new,courier,monospace':'Courier New',
                'georgia,palatino,serif':'Georgia',
                'helvetica,sans-serif':'Helvetica',
                'impact,sans-serif':'Impact',
                'lucida console,monaco,monospace':'Lucida console',
                'lucida sans unicode,lucida grande,sans-serif':'Lucida grande',
                'tahoma,sans-serif':'Tahoma',
                'times new roman,times,serif':'Times New Roman',
                'trebuchet ms,lucida grande,verdana,sans-serif':'Trebuchet MS',
                'verdana,geneva,sans-serif':'Verdana'
            }
        }

        this.select = this.domElem.elSelect(opts);

        this.command = function () {
        }

        this.set = function (size) {
            this.rte.history.add();
            var nodes = this.rte.selection.selected({filter:'textContainsNodes'});
            $.each(nodes, function () {
                $this = /^(THEAD|TFOOT|TBODY|COL|COLGROUP|TR)$/.test(this.nodeName) ? $(this).find('td,th') : $(this);
                $(this).css('font-family', size).find('[style]').css('font-family', '');
            });
            this.rte.ui.update();
        }

        this.update = function () {
            this.domElem.removeClass('disabled');
            var n = this.rte.selection.getNode();
            if (n.nodeType != 1) {
                n = n.parentNode;
            }
            var v = $(n).css('font-family');
            v = v ? v.toString().toLowerCase().replace(/,\s+/g, ',').replace(/'|"/g, '') : '';
            this.select.val(opts.src[v] ? v : '');
        }
    }

})(jQuery);